﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using LIB.Core;
using LIB.Data;
using System.Data;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class wtkb
{
    [OperationContract]
    public string Fjson_GiangDuong_Lke()
    {
        try
        {
            DataTable b_dt = SqlData.GetData("PGIANG_DUONG_LLKE");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fjson_Giang_Duong_NH(string b_soid, string b_ten, string b_vri)
    {
        try
        {
            SqlData.ExeCuteProcedure(new object[] { b_soid, b_ten, b_vri }, "PGIANG_DUONG_NH");
            return Fjson_GiangDuong_Lke();
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fjson_GiangDuong_Xoa(string b_so_id)
    {
        try
        {
            DataTable b_dt = SqlData.GetData(new object[] { b_so_id }, "PGIANG_DUONG_XOA");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    [OperationContract]
    public string Fjson_Khoa_Lke(string id_ht_dt)
    {
        try
        {
            DataTable b_dt_khoa = c_ht.Fdt_KHOA_HEDAOTAO(id_ht_dt);
            return LibBang.Fs_BANG_JSON(b_dt_khoa);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    [OperationContract]
    public string Fjson_Khoa_NH(string b_id, string b_ten, string b_gchu, string b_id_ht_dt)
    {
        try
        {
            SqlData.ExeCuteProcedure(new object[] { b_id, b_ten, b_gchu, b_id_ht_dt }, "PKHOA_NH");
            return Fjson_Khoa_Lke(b_id_ht_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fjson_Khoa_Xoa(string b_id, string b_ht_dt)
    {
        try
        {

            SqlData.ExeCuteProcedure(new object[] { b_id }, "PKHOA_XOA");
            return Fjson_Khoa_Lke(b_ht_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    [OperationContract]
    public string Fjson_HeDaoTao_Lke()
    {
        try
        {

            DataTable b_dt_dt = LIB.Data.SqlData.GetData("PHinhThuc_DT_LKE");
            return LibTable.FormTableToJson(b_dt_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    //Lietj kê
    [OperationContract]
    public string Fs_xep_mon_ttchung()
    {
        try
        {
            //Môn
            DataTable b_dt_bm = c_ht.Fdt_BOMON_LKE();
            //nam hoc
            DataTable b_dt_nam = c_ht.Fdt_NAM_HOC();
            //Br xung phòng
            DataTable b_dt_ph = LIB.Data.SqlData.GetData("PPHONG_BAN_LKE");
            //Ngành đào tạo
            DataTable b_dt_nganh = c_ht.Fdt_NGANH_HOC_LKE();
            //Hình thức đào tạo
            DataTable b_dt_dt = LIB.Data.SqlData.GetData("PHinhThuc_DT_LKE");
            //Loại đào tạo 
            DataTable b_dt_ht = LIB.Data.SqlData.GetData("PDT_HE_LKE");
            //Giảng đường
            string b_gduong = Fjson_GiangDuong_Lke();
            //Khóa học
            string he = "";
            if (!LibTable.IsNullOrEmtyTable(b_dt_dt))
            {
                he = b_dt_dt.Rows[0][0].ToString();
            }
            string khoa = Fjson_Khoa_Lke(he);

            //tungns - Hệ đào tạo



            return LibTable.FormTableToJson(b_dt_bm) + "$" + LibTable.FormTableToJson(b_dt_nam)
                    + "$" + LibTable.FormTableToJson(b_dt_ph)
                        + "$" + LibTable.FormTableToJson(b_dt_nganh) + "$" + LibTable.FormTableToJson(b_dt_dt) + "$" +
                        LibTable.FormTableToJson(b_dt_ht) + "$" + b_gduong + "$" + khoa + "$" + LibTable.FormTableToJson(b_dt_dt);


        }

        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_MonHoc(string id_nganh)
    {
        try
        {
            DataTable b_dt = c_ht.Fdt_MONHOC_NGANH_LKE(id_nganh);
            DataTable b_dt2 = c_ht.Fdt_NGANH_HOC_LKE();

            //Them
            LibTable.AddNewCol(ref b_dt, new string[] { "id_xep", "so_tiet", "hso" }, new object[] { "", "", "1" });
            return LibTable.FormTableToJson(b_dt) + "$" + LibTable.FormTableToJson(b_dt2);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }


    [OperationContract]
    public string Fs_XEP_MON_LKE( string b_he, string b_khoa, string id_nganh, string b_nam, string b_hk)
    {
        try
        {
            luu.ht_nsd _nsd = new luu.ht_nsd();
            DataTable b_dt = SqlData.GetData(new object[] { _nsd.id_user, b_he, b_khoa, id_nganh, b_nam, b_hk }, "PXEP_MON_LKE");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_XEP_MON_NH(string[] a_chon, string hedt, string khoa, string nganh, string nam, string hk, string[] a_mon, string[] a_ten)
    {
        try
        {
            luu.ht_nsd _nsd = new luu.ht_nsd();
            if (_nsd == null) throw new Exception("Mất kết nối. Đăng nhập lại");
            DataTable b_dt = new DataTable();

            LibBang.P_THEM_COT(ref b_dt, new string[] { "nsd", "chon", "id_dt", "khoa", "id_bm", "id_nam", "id_hk", "id_mh", "ten" });
            for (int i = 0; i < a_mon.Length; i++)
            {
                object[] a_gtri = new object[b_dt.Columns.Count];
                a_gtri[0] = _nsd.id_user;
                a_gtri[1] = a_chon[i];
                a_gtri[2] = hedt;
                a_gtri[3] = khoa;
                a_gtri[4] = nganh;
                a_gtri[5] = nam;
                a_gtri[6] = hk;
                a_gtri[7] = a_mon[i];
                a_gtri[8] = a_ten[i];

                LibTable.AddNewRow(ref b_dt, a_gtri);
            }
            SqlData.ExeCuteProcedure(b_dt, "PXEP_MON_NH");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    //b_nganh, b_nam, b_hk, b_khoa, a_xepid
    [OperationContract]
    public string Fs_XEP_MON_XOA(string id_nganh, string b_nam, string b_hk, string b_khoa, string b_hedt, string[] a_xepID)
    {
        try
        {
            DataTable b_dt = new DataTable();
            luu.ht_nsd _nsd = new luu.ht_nsd();
            LibBang.P_THEM_COT(ref b_dt, new string[] {"nsd", "id_xep"});
            for (int i = 0; i < a_xepID.Length; i++)
            {
                string[] a_gtri = new string[b_dt.Columns.Count];
                a_gtri[0] = a_xepID[i];
                a_gtri[1] = b_nam;
                LibTable.AddNewRow(ref b_dt, a_gtri);
                return Fs_XEP_MON_LKE(b_hedt, b_khoa, id_nganh, b_nam, b_hk);
            }
            SqlData.ExeCuteProcedure(b_dt, "PXEP_MON_XOA");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_PHAN_CONG_NS_LKE(string id_phong)
    {
        try
        {
            DataTable b_dt = SqlData.GetData(id_phong, "PPHAN_CONG_NS_TIM");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_LOP_NGANHDT(string id_nganh)
    {
        try
        {
            DataTable b_dt = c_ht.Fdt_LOP_HOC_LKE(id_nganh);
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Nhập
    [OperationContract]
    public string Fs_PC_giang_NH(string nam_hoc, string id_hk, string id_nganh, string id_ns, string id_lop, string[] id_mon, string[] so_tiet, string[] a_hso, string[] a_loai)
    {
        try
        {
            DataTable b_dt = new DataTable();
            luu.ht_nsd nsd = new luu.ht_nsd();
            LibBang.P_THEM_COT(ref b_dt, new string[] { "id_pc", "id_namhoc", "id_hk", "id_nganh", "id_ns", "id_lop", "id_mon", "so_tiet", "hso", "loai", "nsd" });
            for (int i = 0; i < id_mon.Length; i++)
            {
                string[] a_gtri = new string[b_dt.Columns.Count];
                a_gtri[0] = "0";
                a_gtri[1] = nam_hoc;
                a_gtri[2] = id_hk;
                a_gtri[3] = id_nganh;
                a_gtri[4] = id_ns;
                a_gtri[5] = id_lop;
                a_gtri[6] = id_mon[i];
                a_gtri[7] = so_tiet[i];
                a_gtri[8] = a_hso[i];
                a_gtri[9] = a_loai[i];
                a_gtri[10] = nsd.user;

                LibBang.P_THEM_HANG(ref b_dt, a_gtri);
            }
            SqlData.ExeCuteProcedure(b_dt, "PPHAN_CONG_NS_NH");
            return Fs_DS_PC_DAY(nam_hoc, id_hk, id_lop);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }

    }
    [OperationContract]
    public string Fs_PC_DAY_XOA(string id_pc)
    {
        try
        {
            SqlData.ExeCuteProcedure(id_pc, "PPC_GV_XOA");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_DS_PC_DAY(string b_nam, string b_hk, string id_lop)
    {
        try
        {
            DataTable b_dt = SqlData.GetData(new object[] { b_nam, b_hk, id_lop }, "PPC_GV_LKE");


            //lấy bộ môn đã được chọn
            DataTable b_dt2 = SqlData.GetData(new object[] { null, b_nam, b_hk }, "PXEP_MON_LKE");
            LibTable.AddNewCol(ref b_dt2, new string[] { "so_tiet", "hso" }, new object[] { "", "1" });

            return LibTable.FormTableToJson(b_dt) + "$" + LibTable.FormTableToJson(b_dt2);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_TKB_LKE(string id_nganh, string b_nam, string b_hk)
    {
        try
        {
            DataTable b_dt = SqlData.GetData(new object[] { id_nganh, b_nam, b_hk }, "PTKB_NAM_TIM");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_LOP_KHOA(string id_khoa)
    {
        try
        {
            DataTable b_dt_lop = c_ht.Fdt_LOP_HOC_KHOA_LKE(id_khoa);
            return LibBang.Fs_BANG_JSON(b_dt_lop);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    //Lấy thông tin môn học theo bộ môn
    [OperationContract]
    public string Fs_MM_BOMON(string id_bm, string id_hk, string id_namhoc, string id_mon)
    {
        try
        {
            DataTable b_dt_mon = new DataTable();
            b_dt_mon = c_ht.Fdt_MON_NGANH(id_bm);
            return LibBang.Fs_BANG_JSON(b_dt_mon);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

    }
    //Gảng viện giảng dậyPPC_GIANG_DAY_LKE

    [OperationContract]
    public string Fjson_GIANG_DAY_LKE(string id_nam, string id_hk, string id_lop, string id_mon, string b_gv)
    {
        try
        {
            DataTable b_dt_mon = SqlData.GetData(new object[] { id_nam, id_hk, id_lop, id_mon, b_gv }, "PPC_GIANG_DAY_LKE");
            string b_tiet = "0";
            string b_loai = "PH";
            string b_hso = "1";
            if (b_dt_mon.Rows.Count > 0)
            {
                b_tiet = LibConvert.toString(b_dt_mon.Rows[0]["so_tiet"]);
                b_loai = LibConvert.toString(b_dt_mon.Rows[0]["loai"]);
                b_hso = LibConvert.toString(b_dt_mon.Rows[0]["hso"]);
            }
            return b_tiet + "#" + b_loai + "#" + b_hso;
        }
        catch (Exception ex)
        {
            return "loi" + ex.Message;
        }

    }
    //
    [OperationContract]
    public string Fjson_GIANG_DAY_GVIEN_LKE(string id_nam, string id_hk, string id_lop, string id_mon)
    {
        try
        {
            DataTable b_dt_mon = SqlData.GetData(new object[] { id_nam, id_hk, id_lop, id_mon }, "PPC_GIANG_DAY_LKE_GV");
            return LibTable.FormTableToJson(b_dt_mon);
        }
        catch (Exception ex)
        {
            return "loi" + ex.Message;
        }

    }
    //Lấy thông tin phân công giảng dậy lên TKB
    [OperationContract]
    public string Fjson_TKB_PCGD(string b_nam, string b_hk, string b_lop)
    {
        DataSet b_ds = SqlData.GetMutilData(new object[] { b_nam, b_hk, b_lop }, "PTKB_PC_LKE");

        DataTable b_dt_mon = b_ds.Tables[0];

        DataTable b_dt_ns = b_ds.Tables[1];

        return LibTable.FormTableToJson(b_dt_mon) + "$" + LibTable.FormTableToJson(b_dt_ns);
    }

    //Tìm thời khoa biểu
    [OperationContract]
    public string Fjson_TKB_MOI_TIM(string b_nam, string b_hk, string b_ht_dt, string b_he_dt, string b_lop)
    {
        DataTable b_dt = SqlData.GetData(new object[] { b_nam, b_hk, b_ht_dt, b_he_dt, b_lop }, "PTKB_MOI_TIM");
        return LibTable.FormTableToJson(b_dt);
    }
    //Nhập thời khóa biue
    [OperationContract]
    public string Fs_TKB_MOI_NH(string b_so_id, string hso, string loai, object[] a_gtri, string[] a_ten)
    {
        try
        {

            luu.ht_nsd nsd = new luu.ht_nsd();
            DataTable b_dt = new DataTable();
            b_dt = LibTable.CreateTable(a_ten, a_gtri);
            if (b_dt.Rows.Count == 0) throw new Exception("Không có số liệu");

            DataRow _row = b_dt.Rows[0];
            object[] a_value = { b_so_id.ToString(), _row["lop"], _row["mon_hoc"], _row["giang_vien"], _row["ht_dt"], _row["he_dt"], _row["nam_hoc"], _row["hoc_ky"] 
                              ,_row["giang_duong"], _row["so_tiet"], _row["thu"], _row["tg_tu"]+" : "+_row["tg_tu"], nsd.user};

            object[] a_tra = SqlData.ExeCuteProcedure(a_value, "@id_tkb", "PTKB_MOI_NH");

            return a_tra[0].ToString() + "$" +
                    Fjson_TKB_MOI_TIM(LibConvert.toString(_row["nam_hoc"]), LibConvert.toString(_row["hoc_ky"]), LibConvert.toString(_row["ht_dt"]),
                    LibConvert.toString(_row["he_dt"]), LibConvert.toString(_row["lop"]));
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    //Chi tiết tkb
    [OperationContract]
    public string Fs_TKB_MOI_CT(string b_so_id)
    {
        try
        {
            DataTable b_dt = SqlData.GetData(b_so_id, "PTKB_MOI_CT");
            if (b_dt.Rows.Count == 0)
                throw new Exception("Không lấy được chi tiết chứng từ");
            string b_tgian = LibConvert.toString(b_dt.Rows[0]["tgian"]);
            string[] a_tgian = b_tgian.Split(':');
            LibTable.AddNewCol(ref b_dt, new string[] { "tg_tu", "tg_den" }, new object[] { a_tgian[0], a_tgian[1] });
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Xóa
    [OperationContract]
    public string Fs_TKB_MOI_XOA(string b_so_id)
    {
        SqlData.ExeCuteProcedure(b_so_id, "PTKB_MOI_XOA");
        return "";
    }
    //DK auto
    [OperationContract]
    public string Fs_DK_AUTO(string id_nam, string lopid, string hkid)
    {
        try
        {
            c_ht.PDANG_KY_HOC_AUTO(id_nam, lopid, hkid);
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
}
